package socket.udp;

import java.io.IOException;
import java.net.*;

/**
 * 客户端实现
 */
public class UDPClient {
    public static void main(String[] args) throws IOException {
        //1、定义服务器地址，端口号，数据
        InetAddress address = InetAddress.getByName("localhost");
        int port = 8800;
        byte[] data = "用户名:fesine;密码:123".getBytes();
        DatagramPacket packet = new DatagramPacket(data,data.length,address,port);
        DatagramSocket socket = new DatagramSocket();
        socket.send(packet);

        /**
         * 接受服务器响应的数据
         */
        //1、创建数据报
        byte[] data2 = new byte[1024];//创建字节数据，指定接受的数据报大小
        packet = new DatagramPacket(data2, data2.length);
        socket.receive(packet);
        String info = new String(data2, 0, packet.getLength());
        System.out.println("我是客户端，服务器响应信息：" + info);
        socket.close();
    }
}
